package com.pmsystem.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.User;
import com.pmsystem.service.UserService;
import com.pmsystem.util.MD5;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;
	
	
	/**
	 * <p>Title: login</p >  
	 * <p>Description: 业主登陆</p >  
	 * @author 泽强
	 * @param request
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value="login",method=RequestMethod.POST)
	public String login(HttpServletRequest request, String username, String password) {
		User user = userService.login(username, password);
		if (user != null) {
			Byte enable = user.getEnable();
			if (enable == 1) {
				request.getSession().setAttribute("user", user);
				return "user/index";
			}
			request.setAttribute("msg", "账号不可用");
		} else {
			request.setAttribute("msg", "账号或密码错误");
		}
		return "user/login";
	}
	/**
	 * <p>Title: update</p >  
	 * <p>Description: 业主更改信息</p >  
	 * @author 泽强
	 * @param user
	 * @return
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	public String update(User user) {
		boolean flag = userService.update(user);
		if(flag) {
			return "success";
		}
		return "error";
	}
	/**
	 * <p>Title: regist</p >  
	 * <p>Description:业主注册</p >  
	 * @author 魏家洛
	 * @param user
	 * @return
	 */
	@RequestMapping("regist")
	@ResponseBody
	public boolean regist(User user) {
		return userService.regist(user);
	}
}
